//===================================================================
// 西交投.NET产品框架 版权所有
//===================================================================
using System;
using System.Collections.Generic;
using XCI.Windows;
using XCI.Sys.Model;
using XCI.Sys.Service;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 系统序列编辑
    /// </summary>
    public partial class FrmSeqEdit : BaseForm
    {
        public FrmSeqEdit(EditFormArgs args)
        {
            InitializeComponent();
            layoutControl.Args = args;
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        private async void FrmSysSeqEdit_Load(object sender, EventArgs e)
        {
            await layoutControl.BindingModelAsync();
        }

        /// <summary>
        /// 保存按钮事件
        /// </summary>
        private async void BtnSave_Click(object sender, EventArgs e)
        {
            await layoutControl.SaveModelAsync();
        }

        /// <summary>
        /// 关闭按钮事件
        /// </summary>
        private void BtnClose_Click(object sender, EventArgs e)
        {
            DialogOk();
        }

        /// <summary>
        /// 绑定表单
        /// </summary>
        private void LayoutPanel_BindingForm(object sender, EditFormPanelBindingEventArgs e)
        {
            e.Task = async () =>
            {
                var model = await SysSeqService.Instance.GetOrCreateAsync(e.Args);
                bindingSource.DataSource = model;
            };
        }

        /// <summary>
        /// 保存表单
        /// </summary>
        private void LayoutPanel_SaveForm(object sender, EditFormPanelSaveEventArgs e)
        {
            e.Task = () => SysSeqService.Instance.SaveAsync(e.Args.Created, (SysSeq)e.Model);
        }

        /// <summary>
        /// 重置表单
        /// </summary>
        private async void LayoutPanel_ResetForm(object sender, EditFormPanelResetEventArgs e)
        {
            bindingSource.DataSource = await SysSeqService.Instance.NewAsync();
            await layoutControl.BindingModelAsync();
        }
    }
}
